class GamesController < ApplicationController
  layout "basic_layout"
  active_scaffold :game do |config|
    config.list.per_page = 20
    config.columns = [:id, :gencode, :name, :a_player, :max_player, :min_price, :max_price, :min_error, :max_error, :min_sold, :max_sold, :user_id, :player_list]
    config.columns[:a_player].label = "Max A"
    config.columns[:max_player].label = "Max B"
    config.create.columns.exclude :id, :gencode, :user_id, :player_list
    config.update.columns.exclude :id, :gencode, :user_id, :player_list
    config.list.columns.exclude :player_list, :user_id
    config.list.sorting = {:id => 'ASC'}
  end

  protected

  def before_create_save(record)
    record.gencode = random_string(20)
    record.user_id = session[:user].id
    record.current_year = 1
    record.market_size = record.max_player * 1000000
    record.market_remain = record.max_player * 1000000
  end

  def after_create_save(record)
    game_setting = GameSetting.new
    game_setting.game_id = record.id
    if record.a_player==0
      game_setting.unit_cost = 20
    end
    game_setting.save

    for i in 1..record.max_player do
      p = Player.new
      p.game_id = record.id
      p.player_type = "B"
      p.name = "Group B#{i}"
      p.gencode = random_string(6)
      p.save

      pb = PlayerBalance.new
      pb.player_id = p.id
      pb.year = record.current_year-1
      pb.cash = game_setting.start_cash
      pb.common_stock = game_setting.start_cash
      pb.common_stock_unit = game_setting.start_common_stock_unit
      pb.equity_per_share = game_setting.start_cash/game_setting.start_common_stock_unit
      pb.save

      pb = PlayerBalance.new
      pb.player_id = p.id
      pb.year = record.current_year
      pb.cash = game_setting.start_cash
      pb.common_stock = game_setting.start_cash
      pb.common_stock_unit = game_setting.start_common_stock_unit
      pb.equity_per_share = game_setting.start_cash/game_setting.start_common_stock_unit
      pb.save
    end

    for i in 1..record.a_player do
      p = Player.new
      p.game_id = record.id
      p.player_type = "A"
      p.name = "Group A#{i}"
      p.gencode = random_string(6)
      p.save

      pb = PlayerBalance.new
      pb.player_id = p.id
      pb.year = record.current_year-1
      pb.cash = game_setting.start_cash
      pb.common_stock = game_setting.start_cash
      pb.common_stock_unit = game_setting.start_common_stock_unit
      pb.equity_per_share = game_setting.start_cash/game_setting.start_common_stock_unit
      pb.save

      pb = PlayerBalance.new
      pb.player_id = p.id
      pb.year = record.current_year
      pb.cash = game_setting.start_cash
      pb.common_stock = game_setting.start_cash
      pb.common_stock_unit = game_setting.start_common_stock_unit
      pb.equity_per_share = game_setting.start_cash/game_setting.start_common_stock_unit
      pb.save
    end

    for i in 1..20 do
      game_error = GameError.new
      game_error.game_id = record.id
      game_error.year = i
      game_error.min_error = record.min_error
      game_error.max_error = record.max_error
      game_error.min_price = record.min_price
      game_error.max_price = record.max_price
      game_error.save
    end
  end

  def after_update_save(record)
    game_setting = GameSetting.find(:first, :conditions=>["game_id=?", record.id])
    if game_setting
      game_setting.destroy
    end

    game_setting = GameSetting.new
    game_setting.game_id = record.id
    if record.a_player==0
      game_setting.unit_cost = 20
    end
    game_setting.save

    ps = Player.find(:all, :conditions=>["game_id=?", record.id])
    ps.each do |p|
      pbs = PlayerBalance.find(:all, :conditions=>["player_id=?", p.id])
      pbs.each do |pb|
        pb.destroy
      end
      p.destroy
    end
    ges = GameError.find(:all, :conditions=>["game_id=?", record.id])
    ges.each do |ge|
      ge.destroy
    end

    for i in 1..record.max_player do
      p = Player.new
      p.game_id = record.id
      p.player_type = "B"
      p.name = "Group B#{i}"
      p.gencode = random_string(6)
      p.save

      pb = PlayerBalance.new
      pb.player_id = p.id
      pb.year = record.current_year-1
      pb.cash = game_setting.start_cash
      pb.common_stock = game_setting.start_cash
      pb.common_stock_unit = game_setting.start_common_stock_unit
      pb.equity_per_share = game_setting.start_cash/game_setting.start_common_stock_unit
      pb.save

      pb = PlayerBalance.new
      pb.player_id = p.id
      pb.year = record.current_year
      pb.cash = game_setting.start_cash
      pb.common_stock = game_setting.start_cash
      pb.common_stock_unit = game_setting.start_common_stock_unit
      pb.equity_per_share = game_setting.start_cash/game_setting.start_common_stock_unit
      pb.save
    end

    for i in 1..record.a_player do
      p = Player.new
      p.game_id = record.id
      p.player_type = "A"
      p.name = "Group A#{i}"
      p.gencode = random_string(6)
      p.save

      pb = PlayerBalance.new
      pb.player_id = p.id
      pb.year = record.current_year-1
      pb.cash = game_setting.start_cash
      pb.common_stock = game_setting.start_cash
      pb.common_stock_unit = game_setting.start_common_stock_unit
      pb.equity_per_share = game_setting.start_cash/game_setting.start_common_stock_unit
      pb.save

      pb = PlayerBalance.new
      pb.player_id = p.id
      pb.year = record.current_year
      pb.cash = game_setting.start_cash
      pb.common_stock = game_setting.start_cash
      pb.common_stock_unit = game_setting.start_common_stock_unit
      pb.equity_per_share = game_setting.start_cash/game_setting.start_common_stock_unit
      pb.save
    end

    for i in 1..20 do
      game_error = GameError.new
      game_error.game_id = record.id
      game_error.year = i
      game_error.min_error = record.min_error
      game_error.max_error = record.max_error
      game_error.min_price = record.min_price
      game_error.max_price = record.max_price
      game_error.save
    end
  end
end
